package com.offcn.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.offcn.order.entity.Cart;
import com.offcn.order.pojo.OrderItem;
import com.offcn.order.service.CartService;
import com.offcn.sellergoods.feign.ItemFeign;
import com.offcn.sellergoods.pojo.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private ItemFeign itemFeign;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<Cart> addGoodsToCartList(Long itemId, Integer num, List<Cart> cartList) {
        Item item = itemFeign.findItemById(itemId).getData();
        // item对应的商家ID
        String sellerId = item.getSellerId();
        // 判断购物车是否为空
        if (cartList == null) {
            cartList = new ArrayList<>();
        }
        // 判断购物车列表中是否有该商品所属的商家
        Cart cart = findCartBySellerId(cartList, sellerId);
        if (cart == null) {  // 没有该商家ID
            cart = new Cart();
            cart.setSellerId(sellerId);
            cart.setSellerName(item.getSeller());
            OrderItem orderItem = createOrderItemByItem(item, num);
            List<OrderItem> orderItems = new ArrayList<>();
            orderItems.add(orderItem);
            cart.setOrderItemList(orderItems);
            // 将购物车加入到购物车列表
            cartList.add(cart);
        } else {  // 有该商家ID  cart不为空
            List<OrderItem> orderItemList = cart.getOrderItemList();
            OrderItem orderItem = findOrderItemByItemId(orderItemList, itemId);
            if (orderItem == null) {  // 不存在
                orderItem = createOrderItemByItem(item, num);
                orderItemList.add(orderItem);
            } else {  // 存在  orderItem不为空
                orderItem.setNum(orderItem.getNum() + num);
                orderItem.setTotalFee(orderItem.getPrice().multiply(new BigDecimal(orderItem.getNum())));
                //如果数量操作后小于等于0，则移除
                if(orderItem.getNum()<=0){
                    cart.getOrderItemList().remove(orderItem);//移除购物车明细
                }
                //如果移除后cart的明细数量为0，则将cart移除
                if(cart.getOrderItemList().size()==0){
                    cartList.remove(cart);
                }
            }
        }
        return cartList;
    }

    // 用来查询购物车列表是否有对应商品所属商家ID
    private Cart findCartBySellerId(List<Cart> cartList,String sellerId){
        for (Cart cart : cartList) {
            if (sellerId.equals(cart.getSellerId())){
                return cart;
            }
        }
        return null;
    }

    // 查询商品列表中是否有对应商品的Id
    private OrderItem findOrderItemByItemId(List<OrderItem> orderItems,Long itemId){
        for (OrderItem orderItem : orderItems) {
            if (orderItem.getItemId().equals(itemId)) {
                return orderItem;
            }
        }
        return null;
    }

    // 根据Item对象创建OrderItem对象
    private OrderItem createOrderItemByItem(Item item,Integer num) {
        OrderItem orderItem = new OrderItem();
        orderItem.setGoodsId(item.getGoodsId());
        orderItem.setItemId(item.getId());
        orderItem.setTitle(item.getTitle());
        orderItem.setPrice(item.getPrice());
        orderItem.setNum(num);
        orderItem.setPicPath(item.getImage());
        orderItem.setSellerId(item.getSellerId());
        orderItem.setTotalFee(item.getPrice().multiply(new BigDecimal(num)));
        return orderItem;
    }

    @Override
    public void saveCartListToRedis(String username, List<Cart> cartList) {
        String cartListStr = JSON.toJSONString(cartList);
        stringRedisTemplate.boundHashOps("cartList").put(username,cartListStr);
    }

    @Override
    public List<Cart> getCartListFromRedis(String username) {
        String cartListStr = (String)stringRedisTemplate.boundHashOps("cartList").get(username);
        return JSON.parseArray(cartListStr,Cart.class);
    }

    @Override
    public Item findItem(Long itemId) {
        Item item = itemFeign.findItemById(itemId).getData();
        return item;
    }
}
